<? 
require("required/dbconnect.php");
require("required/share_function.php");

if(!empty($_POST['paymethod']))
{
	if(empty($_SESSION['cart']))
	{
		header("Location:os_invalid.php?error=3");
	}
	
	require("required/function.php");
	require("required/email.php");
	
	$id=$_SESSION['temp_member_id'];
	$get_data=mysql_query("select * from os_member where id=$id")or die(mysql_error());
		
	$rd=mysql_fetch_array($get_data,MYSQL_ASSOC);
		
	$name=$rd['name'];
	$hpnumber=$rd['hpnumber'];
	$address1=$rd['address1'];
	$address2=$rd['address2'];
	$postcode=$rd['postcode'];
	$country=$rd['country'];
	$city=$rd['city'];
	$state=$rd['state'];
	$email=$rd['email'];
	$message=$rd['message'];
	$service_preferred=$rd['service_preferred'];
	$paymethod=$_POST['paymethod'];
	$member_type=$rd['member_type'];
	
	$current_date=date('Y-m-d');
	
	$get_counter=mysql_query("select * from os_counter where counter_type='OR' and counter_date='$current_date'")or die(mysql_error());
	
	if(mysql_num_rows($get_counter)==0)
	{
		mysql_query("insert into os_counter(counter_type,counter_date,total_count)values('OR','$current_date','1')")or die(mysql_error());
		$total_count=1;
	}
	else
	{
		$total_count=mysql_result($get_counter,0,"total_count")+1;
		mysql_query("update os_counter set total_count='$total_count' where counter_date='$current_date' and counter_type='OR'")or die(mysql_error());
	}
	
	$or_number="OR".leading_zeros($total_count,3)."".date("dmY")."";
	$regisdate=date('Y-m-d');
	
	$chk=mysql_query("select count(*) as result from os_bill_details where order_request_number='$order_request_number'");
	if(mysql_result($chk,0,"result")>0)
	{
		header("Location:os_mybill.php");
	}
	else
	{
		
		
		$insert=mysql_query("insert into os_bill_details(uid,receiver_name,create_date,order_request_number,address1,address2,postcode,city,country,state,contact_number,service_preferred,email,message,verified_date)
							values($id,'$name',now(),'$or_number','$address1','$address2','$postcode','$city','$country','$state','$hpnumber','$service_preferred','$email','$message',now())");
				
		$get_bill=mysql_query("select * from os_bill_details where email='$email' order by id desc limit 1");
		
		$b=mysql_fetch_array($get_bill,MYSQL_ASSOC);
		
		$totalamount=0;
		
		foreach($_SESSION['cart'] as $cartItems)
		{
			$total=0;
			$itemId=$cartItems['id'];
			$qty=$cartItems['quantity'];
			$addon_ids=$cartItems['addon_id'];
			
			$get_item=mysql_query("select pid, unit_price from os_product_inventory where id=$itemId")or die(mysql_error());
			$insert=mysql_query("insert into os_bill_items(bid,pid,item_id,price,quantity)values(".$b['id'].",".mysql_result($get_item,0,"pid").",$itemId,".mysql_result($get_item,0,"unit_price").",".$qty.")");
			$total=$qty*mysql_result($get_item,0,"unit_price");
			$totalamount=$totalamount+$total;
			$bill_items_id=mysql_insert_id();
			
			if(is_array($addon_ids))
			{
					for($addon=0;$addon<count($addon_ids);$addon++)
					{
						$addon_id=$addon_ids[$addon];
						$get_addon=mysql_query("select * from os_products where id='".$addon_id."'");	
						$ap=mysql_fetch_array($get_addon,MYSQL_ASSOC);
						
						$get_addon_items=mysql_query("select * from os_product_inventory where pid='".$addon_id."' order by id asc limit 1")or die(mysql_error());
						$addon_item=mysql_fetch_array($get_addon_items,MYSQL_ASSOC);
						
						$insert=mysql_query("insert into os_bill_items(bid,pid,item_id,addon_for,price,quantity)values(".$b['id'].",".mysql_result($get_addon_items,0,"pid").",".$addon_item['id'].",".$bill_items_id.",".$addon_item['unit_price'].",".$qty.")");
						
						$unit_price=$addon_item['unit_price'];
						$total_price=$qty*$unit_price;
						
						$totalamount=$totalamount+$total_price;
					}
				
			}
			
		}
	
		$postage=getShippingCost($service_preferred,$country,$state);
		
		$discount=0;
		$update=mysql_query("update os_bill_details set status=0,total_amount='$totalamount',transport_fees='$postage',discount='$discount' where id=".$b['id'])or die(mysql_error());
		
		unset($_SESSION['cart']);
		
		unset($_SESSION['total_items']);
		$_SESSION['order_id']=$b['id'];
		
		$get_payment_setting=mysql_query("select ebank_allowed,paypal_allowed from os_contactinfo where id=1");
    	$allowed=mysql_fetch_array($get_payment_setting,MYSQL_ASSOC);
	
		header("Location:os_confirmbill2.php");
	}
}
else
{	
	header("Location:os_invalid.php");
}

?>
